package com.sskj.one.mine;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.TimePickerView;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.one.R;
import com.sskj.one.bean.HistoryBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/05/22
 */
public class CheckTransactionActivity extends BaseActivity<CheckTransactionPresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.ll_start)
    LinearLayout llStart;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.ll_end)
    LinearLayout llEnd;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;

    private BaseAdapter<HistoryBean> adapter;
    private int page = 1;
    private int size = 10;

    private String startTime;
    private String endTime;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_check_transaction;
    }

    @Override
    public CheckTransactionPresenter getPresenter() {
        return new CheckTransactionPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("交易查询");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintColor(Color.TRANSPARENT)
                .setPaintWidth((int) RudenessScreenHelper.dp2px(this, 10))
                .setFirstDraw(true)
                .setLastDraw(true));


        adapter = new BaseAdapter<HistoryBean>(R.layout.item_check_transaction, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, HistoryBean item) {
                holder.setText(R.id.tv_look_time, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000))
                        .setText(R.id.tv_status, "1".equals(item.getOtype()) ? "买涨" : "买跌")
                        .setTextColor(R.id.tv_status, "1".equals(item.getOtype()) ?
                                ContextCompat.getColor(App.INSTANCE, R.color.color_red) : ContextCompat.getColor(App.INSTANCE, R.color.theme))
                        .setText(R.id.tv_cycle, item.getCycle_time() + "s")
                        .setText(R.id.tv_close_time, TimeFormatUtil.formatE(Long.valueOf(item.getSelltime()) * 1000))
                        .setText(R.id.tv_money, "$" + NumberUtil.keep(item.getTotalprice(), 5))
                        .setText(R.id.tv_xia_rate, item.getBuyprice())
                        .setText(R.id.tv_close_rate, item.getSellprice())
                        .setText(R.id.tv_earn, "$" + NumberUtil.keep(item.getProfit(), 5));
            }
        };
    }

    @Override
    public void initData() {
        startTime();
        ClickUtil.click(llStart, () -> {  // 起始日期
            startTime(true, tvStartTime);
        });
        ClickUtil.click(llEnd, () -> {  // 截止日期
            startTime(false, tvEndTime);
        });
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getHistoryBean(false,startTime,endTime, page, size);

        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getHistoryBean(false,startTime,endTime, page, size);
        });

    }


    public void startTime(boolean flag, TextView view) {  // true起始时间
        TimePickerView mTimePickerView = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        mTimePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
                String format1 = format.format(date);
                view.setText(format1);
                if (flag) {
                    startTime = format1;
                } else {
                    endTime = format1;
                    if (!TimeFormatUtil.isTrueDateTwo(startTime, endTime)) {
                        ToastUtils.show("截止日期小于起始日期，请重新选择");
                        return;
                    }
                }
                page = 1;
                mPresenter.getHistoryBean(true,startTime,endTime, page, size);
            }
        });
        mTimePickerView.show();
    }

    //默认时间加载
    public void startTime() {
        endTime = TimeFormatUtil.getCurrentDate(TimeFormatUtil.FORMAT_B);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.MONTH, -1);
        startTime = TimeFormatUtil.getDateFormat(ca.getTime(), TimeFormatUtil.FORMAT_B);
        tvStartTime.setText(startTime);
        tvEndTime.setText(endTime);
        mPresenter.getHistoryBean(false,startTime,endTime, page, size);
    }


    public void getDataOK(List<HistoryBean> data) {
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, CheckTransactionActivity.class);
        context.startActivity(intent);
    }
}
